//
//  WFSelectedView.m
//  FAKER
//
//  Created by Jianwei on 15/11/5.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "WFSelectedView.h"
#import "FitHeader.h"
#import "AllHeader.h"

#define HEAD_SECTION_URLSTRING @"http://iphone.myzaker.com/zaker/find_promotion.php?_appid=iphone"
@implementation WFSelectedView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _imageDataSourceArray = [NSMutableArray array];
        [self createView];
        [self getImageData];
    }
    return self;
}
- (void)createView
{
    for (NSInteger i = 0; i < 3; i++) {
        BaseClassImageView *imageView = [[BaseClassImageView alloc]init];
        imageView.tag = 1000 + i;
        imageView.userInteractionEnabled = 1;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImageAction:)];
        [imageView addGestureRecognizer:tap];
        if (i == 0) {
            _headImageView = imageView;
        }
        else if(i == 1)
        {
            _leftImageView = imageView;
        }
        else
        {
            _rightImageView = imageView;
        }
    }
    
    [self addSubview:_headImageView];
    [self addSubview:_leftImageView];
    [self addSubview:_rightImageView];
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _headImageView.frame = CGRectMake(5 * FITWIDTH, 5 * FITWIDTH, SCREEN_WIDTH - 10 * FITWIDTH, 67.5 * FITHEIGHT);
    
    
    _leftImageView.frame = CGRectMake(_headImageView.X, _headImageView.Y + _headImageView.HEIGHT + 5 * FITWIDTH, (SCREEN_WIDTH - 15 * FITWIDTH) / 2, _headImageView.HEIGHT);
    
    _rightImageView.frame = CGRectMake(_leftImageView.X + _leftImageView.WIDTH + 5 * FITWIDTH,_leftImageView.Y, _leftImageView.WIDTH, _leftImageView.HEIGHT);
//    _rightImageView.layer.borderWidth = .5;
//    _rightImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
}
- (void)getImageFromDataArray:(NSArray *)dataArray
{
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:[dataArray[0] promotion_img]] placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
    
    [_leftImageView sd_setImageWithURL:[NSURL URLWithString:[dataArray[1] promotion_img]] placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
    
    [_rightImageView sd_setImageWithURL:[NSURL URLWithString:[dataArray[2] promotion_img]] placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
}
- (void)getImageData
{
    [InternetDataRequest getDataByString:HEAD_SECTION_URLSTRING BodyDic:nil WithDataBlock:^(id data) {
        NSArray *array = [[data objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *dic in array[0]) {
            WFSelectedViewModel *model = [[WFSelectedViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_imageDataSourceArray addObject:model];
        }
        for (NSDictionary *dic in array[1]) {
            WFSelectedViewModel *model = [[WFSelectedViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_imageDataSourceArray addObject:model];
        }
        [self getImageFromDataArray:_imageDataSourceArray];
    }];
}
- (void)clickImageAction:(UITapGestureRecognizer *)tap
{
    if (tap.view.tag == 1000) {
        [self.delegate pushViewWithWFSelectViewModel:_imageDataSourceArray[0]];
    } else if(tap.view.tag == 1001)
    {
        [self.delegate pushViewWithWFSelectViewModel:_imageDataSourceArray[1]];
    }
    else
    {
        [self.delegate pushViewWithWFSelectViewModel:_imageDataSourceArray[2]];
    }
}
@end
